import base64
import os
import re
import time
from threading import Lock

from mysql.connector.pooling import MySQLConnectionPool

import request
import json
import requests
import mysql.connector

url = 'https://5g.yiqiu.com/cmsv3api/h5/getDetailByCategorySub/%d/%d/20'

headers = '''
Host: 5g.yiqiu.com
Connection: keep-alive
Accept: application/json, text/plain, */*
User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36
Sec-Fetch-Site: same-origin
Sec-Fetch-Mode: cors
Sec-Fetch-Dest: empty
Referer: https://5g.yiqiu.com/
Accept-Encoding: gzip, deflate, br
Accept-Language: zh-CN,zh;q=0.9,en;q=0.8
Cookie: hideWarningBox=1
'''

image_list_url = "https://5g.yiqiu.com/cmsv3api/h5/getInforDetailById/%d/img"

image_host = "https://img2.feiyulu.com"

# 21 22 23 24 25 26 27 28

base_path = "F:\\download\\python_download\\图片下载\\"

local_path = base_path + "%s\\%d-%d-%d-%d.jpg"

table_name = "C67149_image"

update_sql = 'UPDATE %s SET aid = %d,offset = %d,position = %d where cid = %d'

max_time = "2021-07-19 00:00:00"


class C67149ImageRequest:
    def __init__(self, cid: int, mysql_pool: MySQLConnectionPool, lock: Lock):
        self.cid = cid
        self.mysql_pool = mysql_pool
        self.lock = lock
        self.last_image = ''  # 排重
        self.max_time = time.mktime(time.strptime(max_time, "%Y-%m-%d %H:%M:%S"))
        if self.lock.acquire():
            try:
                con = self.mysql_pool.get_connection()
                mycursor = con.cursor()
                mycursor.execute("select aid,offset,position from %s where cid = %d" % (table_name, self.cid))
                result = mycursor.fetchall()
                self.id = result[0][0]
                self.offset = result[0][1]
                self.position = result[0][2]
                print("数据库：cid=%d,id=%d,offset=%d,position=%d" % (self.cid, self.id, self.offset, self.position))
                mycursor.close()
                con.close()
                if self.offset == 0:
                    self.offset = 1
            except BaseException as ex:
                print(ex)
            finally:
                self.lock.release()

    def get_folder(self):
            return '唯美图片'

    def get_file_name(self):
        return str(self.cid) + "-" + str(self.id) + "-" + str(self.offset) + "-" + str(self.position)

    def get_url(self):
        return url % (self.cid, self.offset)

    def get(self, offset):
        self.offset = offset
        url_new = self.get_url()
        print('读取列表:' + url_new)
        text = request.request_data(url_new, headers)
        if text == "-1":
            return
        content = json.loads(text)
        if content['code'] != 0:
            print("获取列表失败")
            return
        data_list = content.get('data').get('list')
        for item in data_list:
            try:
                created = time.mktime(time.strptime(item['created'], "%Y-%m-%d %H:%M:%S"))
                if created < self.max_time:
                    return
                id = item['id']
                if 0 < self.id < id:
                    print("当前cid=" + str(self.cid) + ",id=" + str(id) + ",跳过当前id")
                    continue
                self.id = id
                self.download()
                self.position = 0
            except BaseException as ex:
                print(ex)
        page_num = content.get('data').get('pageNum')
        pages = content.get('data').get('pages')
        if page_num < pages:
            self.get(page_num + 1)

    def next_data(self):
        self.get(self.offset)
        pass

    def download(self):
        folder = self.get_folder()
        result = request.request_data(image_list_url % self.id, None)
        if result == "-1":
            return
        json_data = json.loads(result)
        if json_data['code'] != 0:
            return
        content = str(json_data['data']['dynamic']['content']).replace(",oc", '\\u').encode('utf-8').decode(
            'unicode_escape')
        rex = "/upload/.*?.txt"  # 正则表达
        try:
            pattern = re.compile(pattern=rex, flags=re.S)
            match = pattern.findall(content)
            for i in range(len(match)):
                data = match[i]
                try:
                    if self.position > i:
                        print("当前cid=" + str(self.cid) + ",id=" + str(self.id) + ",跳过当前position=" + str(i))
                        continue
                    self.position = i
                    image = image_host + str(data)
                    r = requests.get(image, headers=None, stream=True, timeout=10)
                    if r.status_code == 200:
                        str_64 = base64.b64decode(r.content).decode("utf-8")
                        if str_64 == self.last_image:
                            print("重复图片,跳过：" + image)
                            continue
                        self.last_image = str_64
                        str_64 = str_64.split(",")[1]
                        d = base64.b64decode(str_64)
                        file = local_path % (folder, self.cid, self.id, self.offset, self.position)
                        # 判断文件是否存在，不存在则创建
                        if not os.path.exists(base_path + self.get_folder()):
                            os.makedirs(base_path + self.get_folder())
                        with open(file, "wb") as f:
                            f.write(d)
                            print("写入：" + file)
                        if self.lock.acquire():
                            try:
                                con = self.mysql_pool.get_connection()
                                mycursor = con.cursor()
                                mycursor.execute(
                                    update_sql % (table_name, self.id, self.offset, self.position, self.cid))
                                con.commit()
                                mycursor.close()
                                con.close()
                            except BaseException as ex:
                                print(ex)
                            finally:
                                self.lock.release()

                    r.close()
                    del r
                except BaseException as ex:
                    print(ex)
        except BaseException as ex:
            print(ex)
